package store.Service;

import store.Index;
import store.entry.Goods;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Scanner;

public class BuyCar {
    static Scanner sc = new Scanner(System.in);
    public static ArrayList<Goods> buyCarArr = new ArrayList<>();
    public static void buyCar() {
        showBuyCar();
    }
    //显示购物车
    public static void showBuyCar1() {
        System.out.println("编号\t\t名称\t\t价格\t\t购物车数量");
        for (Goods goods : buyCarArr) {
            System.out.println(goods.getId() + "\t" + goods.getName() + "\t" + goods.getPrice() + "\t" + goods.getNum());
        }
    }

    public static void showBuyCar() {
        if (!buyCarArr.isEmpty()) {
            System.out.println("您的购物车里的商品如下：");
            showBuyCar1();
            //定义索引
            int index = 0;
            //定义价格和 数量
            double totalPrice = 0;
            String id;
            int num;
            do {
                System.out.print("请输入要购买的商品编号：");
                id = sc.next();
                //找到付款商品的价格
                for (int i = 0; i < buyCarArr.size(); i++) {
                    if (buyCarArr.get(i).getId().equals(id)){
                        totalPrice = buyCarArr.get(i).getPrice();
                        index = i;
                    }
                }
            }while (idRight(id));
            do {//数量对了那就是num
                System.out.print("请输入要购买的数量：");
                num = sc.nextInt();
            }while (numRight(id,num));
            System.out.println("总金额为：" + totalPrice*num);
            System.out.println("您是否付款? (是Y 否任意键)");
            String choice = sc.next();
            if (choice.equals("Y")) {//如果愿意
                System.out.println("请输入要付款的金额:");
                while (true) {
                    double pay = sc.nextDouble();
                    if (pay >= totalPrice) {
                        double money = pay - totalPrice;
                        BigDecimal money1 = BigDecimal.valueOf(money).setScale(2,4);//保留两位小数，四舍五入
                        System.out.println("付款成功找零:" + money1);
                        //商店减库存
                        for (Goods goods : Store.goodsArr) {
                            if (goods.getId().equals(id)){
                                goods.setNum(goods.getNum()-num);
                            }
                        }
                        //购物车减库存
                        buyCarArr.get(index).setNum(buyCarArr.get(index).getNum()-num);
                        break;
                    }else {
                        System.out.println("付款金额不足，请重新付款：");
                    }
                }
            }
            Index.Indexstore();
        }
        else {
            System.out.println("您的购物车为空");
            Index.Indexstore();
        }
    }
    //判断id
    public static boolean idRight(String id) {
        //判断id
        for (Goods goods : buyCarArr) {
            //当商品id存在时，返回false结束方法的循环
            if (goods.getId().equals(id)) {
                return false;
            }
        }
        System.out.println("您输入的商品编号不存在");
        return true;
    }
    //判断数量
    public static boolean numRight(String id,int num) {
        boolean flag = false;
        if (num<=0){
            System.out.println("请输入正确的数量");
            return true;
        }
        for (Goods goods : buyCarArr) {
            if (goods.getId().equals(id)) {
                //当商品数量不足时，返回true结束方法的循环
                if (goods.getNum() < num) {
                    System.out.println("商品数量不足,商品剩余："+goods.getNum());
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }
}
